package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.base.QueryObject;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.domain.VideoAuth;
import cn.wolfcode.p2p.base.mapper.VideoAuthMapper;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.DateUtil;
import cn.wolfcode.p2p.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class VideoAuthServiceImpl implements IVideoAuthService {
    @Autowired
    private VideoAuthMapper videoAuthMapper;
    @Autowired
    private IUserInfoService userInfoService;

    @Override
    @Transactional(readOnly = true)
    public PageInfo<VideoAuth> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<VideoAuth> list = videoAuthMapper.selectList(qo);
        return new PageInfo<VideoAuth>(list);
    }

    @Override
    public VideoAuth get(Long videoAuthId) {
        return videoAuthMapper.selectById(videoAuthId);
    }

    // 时间拼接
    @Override
    public void apply(String appointmentDate, String appointmentTime, Long customServiceId) {
        UserInfo current = userInfoService.getCurrent();
        // 没有认证过的才去获取
        if (!current.getHasVideoAuth()) {
            VideoAuth va = new VideoAuth();
            va.setState(VideoAuth.STATE_NORMAL);
            va.setApplyTime(new Date());
            //--------------客服----------------------
            LoginInfo auditor = new LoginInfo();
            auditor.setId(customServiceId);
            va.setAuditor(auditor);
            //---------------申请人------------------
            va.setApplier(UserContext.getCurrentUser());
            //----------------设置时间------------------
            String[] times = appointmentTime.split("-");
            String begin = appointmentDate + " " + times[0];
            String end = appointmentDate + " " + times[1];
            Date beginAppointmentTime = DateUtil.string2date(begin, "yyyy-MM-dd HH:mm");
            Date endAppointmentTime = DateUtil.string2date(begin, "yyyy-MM-dd HH:mm");
            va.setBeginAppointmentTime(beginAppointmentTime);
            va.setEndAppointmentTime(endAppointmentTime);
            videoAuthMapper.insert(va);
            //----------------更新userInfo-----------------
            UserInfo userInfo = userInfoService.get(va.getApplier().getId());
            userInfo.setVideoAuthId(va.getId());
            userInfoService.update(userInfo);
        }
    }

    @Override
    public void audit(Long videoAuthId, int state, String remark) {
        // 1.查询当前视频认证信息,并判断非空,处于待审核状态
        VideoAuth videoAuth = videoAuthMapper.selectById(videoAuthId);
        // 2.将视频申请人的信息查出后,看是否有进行过视频审核申请
        if (videoAuth != null
                && videoAuth.getState() == VideoAuth.STATE_NORMAL
//                && UserContext.getCurrentUser().getId().equals(videoAuth.getAuditor().getId())
        ) {
            UserInfo applierUser = userInfoService.get(videoAuth.getApplier().getId());
            if (!applierUser.getHasVideoAuth()) {
                // 3.设置审核相关的信息,审核时间,备注,状态,审核的人(安全判断需要当前客服必须是选择,更新)
                videoAuth.setState(state)
                        .setRemark(remark)
                        .setAuditTime(new Date());
                videoAuthMapper.updateById(videoAuth);
                if (state == VideoAuth.STATE_PASS) {
                    // 4.审核通过,将userInfo申请人的状态码
                    applierUser.setBitState(applierUser.getNewState(BitStateUtil.OP_VIDEO_AUTH));
                } else if (state == VideoAuth.STATE_REJECT) {
                    // 5.审核失败,要将视频认证状态id设置为null
                    applierUser.setVideoAuthId(null);
                }
            }
            userInfoService.update(applierUser);
        }

    }
}
